//
//  UserDefaultsEx.swift
//  Extend
//
//  Created by shufang zou on 2017/9/26.
//  Copyright © 2017年 敬信. All rights reserved.
//

import UIKit
import SwiftyJSON
import HandyJSON

let defaultStand = UserDefaults.standard

open class UserDefaultsEx: NSObject {
    
    //单存储
    public class func saveString(_ key: String, _ value: String) {
        if key.isEmpty  || value.isEmpty {
            return
        }
        defaultStand.set(value, forKey: key)
        defaultStand.synchronize()
    }
    
    public class func saveInt(_ key: String, _ value: Int) {
        if key.isEmpty {
            return
        }
        defaultStand.set(value, forKey: key)
        defaultStand.synchronize()
    }
    
    public class func saveFloat(_ key: String, _ value: Float) {
        if key.isEmpty {
            return
        }
        defaultStand.set(value, forKey: key)
        defaultStand.synchronize()
    }
    
    public class func saveBool(_ key: String, _ value: Bool) {
        if key.isEmpty {
            return
        }
        defaultStand.set(value, forKey: key)
        defaultStand.synchronize()
    }
    
    
   //单获取
    public class func getString(_ key: String) -> String {
    
        return defaultStand.string(forKey: key) ?? ""
    }
    
    public class func getInt(_ key: String) -> Int {
        return defaultStand.integer(forKey: key)
 
    }
    
    public class func getFloat(_ key: String) -> Float {
        return defaultStand.float(forKey: key)
    }
    
    public class func getBool(_ key: String) -> Bool {
        return defaultStand.bool(forKey: key)
    }
    
    
    //model 存储
    public class func saveModel(_ dic: Dictionary<String, Any>) {
        for keyDic in dic {
            if ((keyDic.value as? Int) != nil) {
                saveInt(keyDic.key, keyDic.value as! Int)
              
            } else if ((keyDic.value as? Float) != nil) {
                saveFloat(keyDic.key, keyDic.value as! Float)
                
            } else if ((keyDic.value as? Bool) != nil) {
                saveBool(keyDic.key, keyDic.value as! Bool)
                
            } else if ((keyDic.value as? String) != nil) {
                saveString(keyDic.key, keyDic.value as! String)
                
            } else if ((keyDic.value as? Dictionary<String, Any>) != nil) {
                saveModel(keyDic.value as! Dictionary<String, Any>)
                
            } else if ((keyDic.value as? Array<Any>) != nil) {
                saveArray(keyDic.key, keyDic.value as! Array<Any>)
            }
        }
    }
    
    open class func saveArray(_ key: String, _ arr: Array<Any>) {
        
    }
    
    //删除
    public class func deleteValue(_ key: String) {
        defaultStand.removeObject(forKey: key)
        defaultStand.synchronize()
    }
    
    //清除数据库
    public class func clearAllUserDefaults(_ type: Bool) {
        let dic = defaultStand .dictionaryRepresentation()
        
        for value in dic{
            if !value.key.isEmpty {
                if !type {
                    if !isDelete(value.key) {
                        continue
                    } 
                }
               deleteValue(value.key)
            }
        }
    }
    
    open class func isDelete(_ key: String) -> Bool {
        return true
    }
}

